package net.javapark.rssreader;

import java.util.List;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
	private MainActivity local;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		local = this;

		GetRSSDataTask task = new GetRSSDataTask();

		String rssUrl = "http://javapark.tistory.com/rss";
		task.execute(rssUrl);

		Log.d("SimpleRssReader", Thread.currentThread().getName());
	}

	private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem>> {

		@Override
		protected List<RssItem> doInBackground(String... urls) {
			Log.d("SimpleRssReader", Thread.currentThread().getName());

			try {
				RssReader rssReader = new RssReader(urls[0]);

				return rssReader.getItems();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(List<RssItem> result) {
			ListView listView = (ListView) findViewById(R.id.listView1);

			ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(local, android.R.layout.simple_list_item_1,
					result);

			listView.setAdapter(adapter);

			listView.setOnItemClickListener(new ListListener(result, local));
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
